# vim:ts=4:sw=4:noet:
""" Primitive method table """

primitives = {
	# Object
	"objectIdentity" : 1,
	"objectClass"    : 2,
	"objectSize"     : 3,
	"objectNew"      : 4,

	# Array
	"arrayAt"        : 5,
	"arrayAtPut"     : 6,
	"bytearrayAlloc" : 7,

	# String
	"stringAt"       : 8,
	"stringAtPut"    : 9,
	"stringClone"    : 10,
	"stringReplace"  : 11,
	"stringPrint"    : 12,

	# Character
	"charPrint"      : 13,
	"charRead"       : 14,

	# Block
	"blockValue"     : 15,
	"contextExecute" : 16,

	# Smallint
	"smallintRandom" : 31,
	"smallintAdd"    : 32,
	"smallintDiv"    : 33,
	"smallintMod"    : 34,
	"smallintLess"   : 35,
	"smallintEqual"  : 36,
	"smallintMult"   : 37,
	"smallintSub"    : 38,
	"smallintAlloc"  : 39,
	"smallintFit"    : 40,
	"smallintBitOr"  : 41,
	"smallintBitAnd" : 42,
	"smallintShift"  : 43,
	"smallintTrunk"  : 44,

	# Integer
	"integerDiv"     : 45,
	"integerMod"     : 46,
	"integerAdd"     : 47,
	"integerMult"    : 48,
	"integerSub"     : 49,
	"integerLess"    : 50,
	"integerEqual"   : 51,

	# System
	"methodFlush"    : 96,
	"halt"           : 97,
	"error"          : 98,
	"yieldUntil"     : 99,

	# Hardware
	"sleepMS"        : 128,
	"timeStartupMS"  : 129,
	"motorVelocity"  : 130,
	"motorBrake"     : 131,
	"analogRead"     : 132,
	"digitalRead"    : 133,
	"setPenState"    : 134,
	"setPenColour"   : 135,
	"memWrite"       : 136,
	"memRead"        : 137,
	"setPosition"    : 138,
	"getPosition"    : 139,
	"getEncoder"     : 140,
	"motionCommand"  : 141,
	"setPenStyle"    : 142,

	# Music
	"musicStop"      : 160,
	"musicPlayNote"  : 161,
	"musicPlayPatch" : 162,
}

class PrimTable:
	def __init__(self):
		pass

	def lookupName(self, name):
		if name in primitives:
			return primitives[name]
		else:
			return None

	def cifyName(self, name):
		new = "PRIM_"
		cp = "a"
		for c in name:
			if c.isupper() and cp.islower():
				new += "_"+c
			else:
				new += c.upper()
			cp = c
		return new

	def writeHFile(self, fn):
		nums = {}
		for name in primitives:
			nums[primitives[name]] = name
		numkeys = nums.keys()
		numkeys.sort()
		f = file(fn,"wb")
		f.write("#ifndef _PRIMDEFS_H_\n")
		f.write("#define _PRIMDEFS_H_\n\n")
		for num in numkeys:
			name = self.cifyName(nums[num])
			f.write("#define %s%s%d\n" % (name," "*(25-len(name)),num))
		f.write("\n#endif\n")

if __name__=="__main__":
	p = PrimTable()
	p.writeHFile("primdefs.h")
